package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.Goods;
import cn.yunlang.zhangZhongFei.entity.UserDislike;
import cn.yunlang.zhangZhongFei.mapper.GoodsMapper;
import cn.yunlang.zhangZhongFei.mapper.UserDislikeMapper;
import cn.yunlang.zhangZhongFei.service.UserDislikeService;
import cn.yunlang.zhangZhongFei.vo.request.UserDislikePageVo;
import cn.yunlang.zhangZhongFei.vo.request.UserDislikeVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 用户拉黑表 服务实现类
 *
 * @author ·
 * @since 2025-05-26 14:50:46
 */
@Service
public class UserDislikeServiceImpl extends ServiceImpl<UserDislikeMapper, UserDislike>
    implements UserDislikeService {

  @Resource private GoodsMapper goodsMapper;

  public R<IPage<UserDislikePageVo>> pageDislike(Integer pageNo, Integer pageSize, Long userId) {
    if (pageNo == null || pageSize == null || userId == null) {
      return R.error(400, "分页参数和用户ID不能为空");
    }
    Page<UserDislike> page =
        this.page(
            new Page<>(pageNo, pageSize),
            new LambdaQueryWrapper<UserDislike>()
                .eq(UserDislike::getUserId, userId)
                .orderByDesc(UserDislike::getCreateTime));
    Page<UserDislikePageVo> pageRes = new Page<>();
    BeanUtils.copyProperties(page, pageRes);
    List<UserDislikePageVo> list = new ArrayList<>();
    List<Long> goodsIdList =
        page.getRecords().stream().map(UserDislike::getCompanyId).collect(Collectors.toList());
    List<Goods> goods = goodsMapper.selectBatchIds(goodsIdList);
    Map<Long, Goods> goodsMap =
        goods.stream().collect(Collectors.toMap(Goods::getId, good -> good));
    page.getRecords()
        .forEach(
            item -> {
              Goods currentGood = goodsMap.get(item.getCompanyId());
              UserDislikePageVo userDislikePageVo = new UserDislikePageVo();
              BeanUtils.copyProperties(item, userDislikePageVo, "createTime");
              if (currentGood != null) {
                BeanUtils.copyProperties(currentGood, userDislikePageVo);
              }
              list.add(userDislikePageVo);
            });
    pageRes.setRecords(list);
    return R.success(pageRes);
  }

  public R<String> doDislike(UserDislikeVo userDislikeVo) {
    if (userDislikeVo == null
        || userDislikeVo.getUserId() == null
        || userDislikeVo.getCompanyId() == null) {
      return R.error(400, "用户ID和公司ID不能为空");
    }
    UserDislike one =
        this.getOne(
            new LambdaQueryWrapper<UserDislike>()
                .eq(UserDislike::getUserId, userDislikeVo.getUserId())
                .eq(UserDislike::getCompanyId, userDislikeVo.getCompanyId()));
    boolean back;
    if (one == null) {
      back = this.dislike(userDislikeVo);
    } else {
      back = this.removeDislike(userDislikeVo);
    }
    return back ? R.success("拉黑成功") : R.error(500, "拉黑失败");
  }

  public R<String> dislikeUser(UserDislikeVo userDislikeVo) {
    if (userDislikeVo == null
        || userDislikeVo.getUserId() == null
        || userDislikeVo.getCompanyId() == null) {
      return R.error(400, "用户ID和公司ID不能为空");
    }
    boolean save = this.dislike(userDislikeVo);
    return save ? R.success("拉黑成功") : R.error(500, "拉黑失败");
  }

  public R<String> removeDislikeUser(UserDislikeVo userDislikeVo) {
    if (userDislikeVo == null
        || userDislikeVo.getUserId() == null
        || userDislikeVo.getCompanyId() == null) {
      return R.error(400, "用户ID和公司ID不能为空");
    }
    boolean remove = this.removeDislike(userDislikeVo);
    return remove ? R.success("取消成功") : R.error(500, "取消失败");
  }

  @Override
  public boolean dislike(UserDislikeVo userDislikeVo) {
    UserDislike userDislike = new UserDislike();
    BeanUtils.copyProperties(userDislikeVo, userDislike);
    return save(userDislike);
  }

  @Override
  public boolean removeDislike(UserDislikeVo userDislikeVo) {
    return remove(
        new LambdaQueryWrapper<UserDislike>()
            .eq(UserDislike::getUserId, userDislikeVo.getUserId())
            .eq(UserDislike::getCompanyId, userDislikeVo.getCompanyId()));
  }
}
